package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.dao.mapper.UserCouponsMapper;
import com.tarena.lbs.coupon.dao.repository.UserCouponsRepository;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: UserCouponsRepositoryImpl
 * @author: Feng-hongliang
 * @date: 2025/10/11 17:55
 * @Version: 1.0
 * @description:
 */
@Slf4j
@Repository("userCouponsRepository")
public class UserCouponsRepositoryImpl implements UserCouponsRepository {
    private final UserCouponsMapper userCouponsMapper;

    public UserCouponsRepositoryImpl(UserCouponsMapper userCouponsMapper) {
        this.userCouponsMapper = userCouponsMapper;
    }

    @Override
    public int insert(UserCouponsPO record) {
        try {
            return userCouponsMapper.insert(record);
        } catch (Exception e) {
            log.error("Failed to insert: " + e.getMessage(), e);
            throw new RuntimeException("Failed to insert coupon code: " + e.getMessage(), e);
        }
    }

    @Override
    public List<UserCouponsPO> findByCouponId(Integer poId) {
        return List.of();
    }

    /**
     *
     * @param couponQuery 查询参数
     * @param couponIds   优惠券ID列表
     * @return
     */
    @Override
    public PageResult<UserCouponsPO> listUserReceivedCouponsByCouponIds(UserCouponQuery couponQuery, List<Integer> couponIds) {
        try {
            // 设置分页参数
            Page<UserCouponsPO> page = new Page<>(couponQuery.getPageNo(), couponQuery.getPageSize());
            QueryWrapper<UserCouponsPO> queryWrapper = new QueryWrapper<>();

            // 根据优惠券ID列表查询
            if (couponIds != null && !couponIds.isEmpty()) {
                queryWrapper.in("coupon_id", couponIds);
            } else {
                // 如果没有优惠券ID，返回空结果
                PageResult<UserCouponsPO> emptyResult = new PageResult<>();
                emptyResult.setObjects(new ArrayList<>());
                emptyResult.setTotal(0L);
                emptyResult.setPageNo(couponQuery.getPageNo());
                emptyResult.setPageSize(couponQuery.getPageSize());
                return emptyResult;
            }

            // 添加其他查询条件（如果UserCouponQuery中有相关字段）
            // 例如：
            // if (couponQuery.getUserId() != null) {
            //     queryWrapper.eq("user_id", couponQuery.getUserId());
            // }
            // if (couponQuery.getStatus() != null) {
            //     queryWrapper.eq("status", couponQuery.getStatus());
            // }

            userCouponsMapper.selectPage(page, queryWrapper);

            // 构建PageResult
            PageResult<UserCouponsPO> pageResult = new PageResult<>();
            pageResult.setObjects(page.getRecords());
            pageResult.setTotal(page.getTotal());
            pageResult.setPageNo((int) page.getCurrent());
            pageResult.setPageSize((int) page.getSize());

            return pageResult;
        } catch (Exception e) {
            log.error("查询用户领取优惠券列表失败", e);
            throw new RuntimeException("查询用户领取优惠券列表失败", e);
        }
    }

    /**
     * 根据优惠券ID列表查询用户领取记录
     * 目前仅实现根据用户id查询
     *
     * @param couponQuery 查询参数
     * @return 用户领取记录
     */
    @Override
    public PageResult<UserCouponsPO> listUserReceivedCoupons(UserCouponQuery couponQuery) {
        try {
            Integer userId = couponQuery.getUserId();
            Page<UserCouponsPO> page = new Page<>(couponQuery.getPageNo(), couponQuery.getPageSize());

            QueryWrapper<UserCouponsPO> queryWrapper = new QueryWrapper<>();

            // 根据用户ID查询
            if (userId != null) {
                queryWrapper.eq("user_id", userId);
            }

            // TODO：添加其他查询条件
            if (couponQuery.getStatus() != null) {
                queryWrapper.eq("status", couponQuery.getStatus());
            }

            userCouponsMapper.selectPage(page, queryWrapper);

            // 构建PageResult
            PageResult<UserCouponsPO> pageResult = new PageResult<>();
            pageResult.setObjects(page.getRecords());
            pageResult.setTotal(page.getTotal());
            pageResult.setPageNo((int) page.getCurrent());
            pageResult.setPageSize((int) page.getSize());

            return pageResult;
        } catch (Exception e) {
            log.error("查询用户领取优惠券列表失败", e);
            throw new RuntimeException("查询用户领取优惠券列表失败", e);
        }
    }

    public PageResult<UserCouponsPO> listUserReceivedCouponsByCouponIds(UserCouponsPO userCouponsPO, List<Integer> couponIds) {
        return null;
    }

}
